package com.example.p4;

import java.util.ArrayList;
import java.util.Arrays;

import com.playtomic.android.api.*;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

//HighscoreActivity will list the highscores to the user 
//utilizing a listview: an expandable and scrollable UI piece
public class ViewHighscoreActivity extends Activity {

	private ListView highscoresList;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_highscores);
        
        //initialize the buttons' click actions
        
       // ArrayList<String> names = new ArrayList<String>(
        	//Arrays.asList("Jason","Daniel", "Jerry", "Taylor", "Sam", "Brian", "Stephen", "Katie", "Elizabeth"));
        
        //Test saving a highscore
        //leaderboardSave(names.get((int)(Math.random()*(names.size()-1))), (int)(Math.random()*100) );
        
        highscoresList = (ListView) findViewById(R.id.high_scores_list);
        
        getAndShowHighScores(highscoresList);
    } 
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    protected void goToMainMenu() {
        finish();
    }
    
    private void showScores(ArrayList<String> scores, ListView list)
    {
    	//TextView text = (TextView)findViewById(R.layout.simplerow);
    	//text.setTextSize(25);
    	ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, scores);  
        highscoresList.setAdapter( listAdapter );
    }
    
    //Playtomic highscores
    
    private void leaderboardSave(String name, int newScore) {
        Log.d("Highscores", "Getting leaderboard");
        PlaytomicLeaderboards leaderboards = new PlaytomicLeaderboards();

        // we need to set a listener
        leaderboards.setRequestListener(new PlaytomicRequestListener<PlaytomicScore>() {
            public void onRequestFinished(PlaytomicResponse<PlaytomicScore> playtomicResponse) {
	                if(playtomicResponse.getSuccess()) {
	                    // we call a function for successed cases
	                	requestLeaderBoardSaveFinished();
	                }            
	                else {
	                    // we call a function for failed cases 
	                    requestLeaderBoardSaveFailed(playtomicResponse.getErrorCode(), 
	                    		playtomicResponse.getErrorMessage());
	                }
            	}
        });
        // create a new score
        
        PlaytomicScore score = new PlaytomicScore(name, newScore);

        // save the score
        
        leaderboards.save("Highscores", score, true, true);
    }

    private void requestLeaderBoardSaveFinished() {
        Log.d("Highscores", "Save score success");
    }
        
    private void requestLeaderBoardSaveFailed(int errorCode, String message) {
        Log.d("Highscores", "Leaderboard save failed to save because of errorcode #" + errorCode 
                +  " - Message:" + message);
    }
    
    
    
    //List leaderboards TEST
    private void getAndShowHighScores(final ListView list) {
        Log.d("Highscores", "Leaderboard List");
        PlaytomicLeaderboards leaderboards = new PlaytomicLeaderboards();
        
        // we need to set a listener
            leaderboards.setRequestListener(new PlaytomicRequestListener<PlaytomicScore>() {
            public void onRequestFinished(PlaytomicResponse<PlaytomicScore> playtomicResponse) {
                if(playtomicResponse.getSuccess()) {
                    // we call a function for successed cases
                	ArrayList<String> scores = requestLeaderBoardListFinished(playtomicResponse.getData());
                	showScores(scores, list);
                }            
                else {
                    // we call a function for failed cases 
                        requestLeaderBoardListFailed(playtomicResponse.getErrorCode(), 
                            playtomicResponse.getErrorMessage());
                }
            }
        });
        leaderboards.list("Highscores", true, "last7days", 1, 25, null);
    }

    private ArrayList<String> requestLeaderBoardListFinished(ArrayList<PlaytomicScore> data) {
    	ArrayList<String> highscoresList = new ArrayList<String>();
        //Log.d("Highscores", "Leaderboard {");

        for(int i=0; i<data.size() && i< 25; i++)
        {
        	PlaytomicScore score = data.get(i);
        	/*Log.d("Highscores entry", "----------------------------------\nScore:\nName=\"" + score.getName() +  "\"");
            Log.d("Highscores entry", "Points=\"" + score.getPoints() +  "\"");
            Log.d("Highscores entry", "Date=\"" + score.getDate() +  "\"");
            Log.d("Highscores entry", "Relative Date=\"" + score.getRelativeDate() +  "\"");
            Log.d("Highscores entry", "Rank=\"" + score.getRank() +  "\"");
            Log.d("Highscores entry", "Custom Data {");
            Log.d("Highscores", "}");*/
            
        	highscoresList.add(score.getName() + ": "+ score.getPoints());
        }

        //Log.d("Highscores", "}");
        return highscoresList;
    }

    private void requestLeaderBoardListFailed(int errorCode, String message) {
        Log.d("Highscores", "Leaderboard list failed to list because of errorcode #" + errorCode 
                +  " - Message: " + message);
    }

//End Listing Leaderboards Test
}

